/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SampleHost;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
/**
 *
 * @author Owner
 */
public class PortWriter {
    private static final int BYTESIZE = 8;
    private OutputStream outputStream;

    public PortWriter(PortObject portObject) throws IOException {
        outputStream = portObject.getPort().getOutputStream();
    }

    public void writeByte(byte byteToWrite) throws IOException {
        outputStream.write(byteToWrite);
    }

    public void writeHalf(short halfToWrite) throws IOException {
        writeByte((byte)halfToWrite);
        writeByte((byte)(halfToWrite>>BYTESIZE));
    }

    public void writeWord(int wordToWrite) throws IOException {
        writeHalf((short)wordToWrite);
        writeHalf((short)(wordToWrite>>2*BYTESIZE));
    }

    public void write(byte[] byteArray) throws IOException {
        for (int i=0; i<byteArray.length; i++) {
            writeByte(byteArray[i]);
        }
    }

    public void write(InputStream inputStream, int count) throws IOException {
        for (;0 < count; count--) {
            byte b = (byte)inputStream.read();
            writeByte(b);
        }
    }
    
    public void write(InputStream inputStream) throws IOException {
        write(inputStream, inputStream.available());
    }
}

